package com.zs.grain.ui

import android.hardware.usb.UsbManager
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.zs.grain.*
import com.zs.grain.databinding.FragmentAdvertisementBinding
import com.xnkj1688.semplibrary.mvvm.*
import com.xnkj1688.semplibrary.recyclerview.ItemView
import android.app.ProgressDialog
import android.content.*
import com.xnkj1688.semplibrary.entity.Advertisement
import com.xnkj1688.semplibrary.recyclerview.ClickHandler

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
@Route(path = ARouterPath.AdvertisementFragment)
class AdvertisementFragment : BaseMvvmFragment<FragmentAdvertisementBinding, AdvertisementViewModel>() {
    override val layoutId = R.layout.fragment_advertisement
    val advertisementItemView = ItemView<String>(BR.vm, R.layout.item_list4)
    override fun initViewModel(binding: FragmentAdvertisementBinding) {
        binding.vm = this.mViewModel
        binding.view = this

        mViewModel.init()
    }

    override fun onResume() {
        super.onResume()
        registerMessageReceiver()
    }

    override fun onPause() {
        super.onPause()
        unregisterReceiver()
    }

    override fun onUiNotify(name: String, bundle: Bundle) {
        super.onUiNotify(name, bundle)
        when (name) {
            "ShowProgress" -> {
                showProgressDialog("广告读取", "读取中......")
            }
            "HideProgress" -> {
                hideProgressDialog()
            }
            "VideoShow" -> {
                showProgressDialog("广告读取", "读取中......")
            }
            "Quit" -> {


            }
        }
    }

    val videoItemView = ItemView<Advertisement>(BR.vm, R.layout.item_list4)
    val videoClickHandler = ClickHandler { vm: Advertisement ->
        mViewModel.onClickVideo(vm)
    }
    private var mMessageReceiver: MessageReceiver? = null

    fun registerMessageReceiver() {
        mMessageReceiver = MessageReceiver()
        val filter = IntentFilter()
        filter.priority = IntentFilter.SYSTEM_HIGH_PRIORITY
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
        filter.addAction(ACTION_USB_PERMISSION)
        this.activity?.registerReceiver(mMessageReceiver, filter)
    }

    fun unregisterReceiver() {
        this.activity?.unregisterReceiver(mMessageReceiver)
    }

    inner class MessageReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            try {
                when (intent.action) {
                    UsbManager.ACTION_USB_DEVICE_ATTACHED ->
                        mViewModel.onUsbAttached(context, intent)
                    ACTION_USB_PERMISSION ->
                        mViewModel.onUsbPermession(context, intent)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    internal var progressingView: ProgressDialog? = null
    // 显示进度
    fun showProgressDialog(title: String, msg: String) {
        if (TextUtils.isEmpty(msg) || TextUtils.isEmpty(title)) {
            return
        }
        if (progressingView == null) {
            progressingView = ProgressDialog(this.activity)//1.创建一个ProgressDialog的实例
        }
        progressingView!!.setTitle(title)//2.设置标题
        progressingView!!.setMessage(msg)
//        progressingView!!.setCancelable(false)//4.设置可否用back键关闭对话框
        progressingView!!.show()
    }

    fun hideProgressDialog() {
        if (progressingView == null) {
            return
        }
        progressingView!!.hide()
    }

    companion object {
        val TAG = "AdvertisementFragment"
        val ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"
    }
}
